let Mock = require('mockjs');
//import base64 from '../../src/utils/base64';
let en = require('../i18n/lang/en')
let Random = Mock.Random;
/*
* 登录
* */
let userLogin = (options) => {
  //获取请求参数
  let data = JSON.parse(options.body);
  let arr = [
    {
      account: "admin",
      password: "123456",
      type: 1,//管理员
      name: "admin",
      userID: "1",
    },
    {
      account: "renshi",
      password: "123456",
      type: 2,//人事部
      name: "王五",
      userID: "2",
    },
    {
      account: "yanfa",
      password: "123456",
      type: 3,//研发部
      name: "李四",
      userID: "3",
    }
  ];
  //登录返回对象
  let req = {code: 0, data: null};
  for (let i = 0; i < arr.length; i++) {
    if (data.account == arr[i].account && data.password == arr[i].password) {
      req.data = arr[i];
      return req;
    } else {
      if (i == arr.length - 1)//判断循环是否结束
        return {code: -1, data: '用户不存在！'}
    }
  }
  //根据token获取用户信息
  /*let str = base64.encode(data);
  let user = str.split(",");
  console.log(user);*/

};


let menuTitle = en.default.menuTitle

//用户权限菜单
let menuList = (options) => {
  //获取请求参数
  let data = JSON.parse(options.body);
  let response = {code: 0, data: []};
  let menuList = [
    {
      id: "1",
      type: 2,
      name: menuTitle.records,
      icon: 'fa fa-envira',
      path: '/index',
    },
    {
      id: "2",
      type: 2,
      name: menuTitle.systemManage,
      icon: 'fa fa-bars',
      children: [
        {
          id: "3",
          type: 1,
          name: menuTitle.shopManagement,
          icon: 'fa fa-paper-plane',
          path: '/shopManagement',
        }
      ],
    },
    {
      id: "4",
      type: 2,
      name: menuTitle.financial,
      icon: 'fa fa-bars',
      //url: '/index',
      children: [
        {
          id: "5",
          type: 1,
          name: menuTitle.FundsManagement,
          icon: 'fa fa-paper-plane',
          path: '/map',
        }
      ],
    },
    {
      id: "6",
      type: 2,
      name: menuTitle.userMessage,
      icon: 'fa fa-cogs',
      //url: '/index',
      children: [
        {
          id: "7",
          type: 1,
          name: menuTitle.UserList,
          icon: 'fa fa-cogs',
          path: '/screen',
        }
      ],
    },
    {
      id: "8",
      type: 2,
      name: menuTitle.invoice,
      icon: 'fa fa-cogs',
      //url: '/index',
      children: [
        {
          id: "9",
          type: 1,
          name: menuTitle.invoiceList,
          icon: 'fa fa-cogs',
          path: '/screen',
        }
      ],
    },
    {
      id: "10",
      type: 2,
      name: menuTitle.orderManagement,//订单管理
      icon: 'fa fa-bars',
      //url: '/index',
      children: [
        {
          id: "11",
          type: 1,
          name: menuTitle.orderList,
          icon: 'fa fa-paper-plane',
          path: '/map',
        },
        {
          id: "12",
          type: 1,
          name: menuTitle.orderDetail,
          icon: 'fa fa-paper-plane',
          path: '/map',
        }
      ],
    },
    {
      id: "13",
      type: 2,
      name: menuTitle.logistics,//物流管理
      icon: 'fa fa-bars',
      //url: '/index',
      children: [
        {
          id: "14",
          type: 1,
          name: menuTitle.logisticsList,
          icon: 'fa fa-paper-plane',
          path: '/map',
        },
        {
          id: "15",
          type: 1,
          name: menuTitle.logisticsDetail,
          icon: 'fa fa-paper-plane',
          path: '/map',
        }
      ],
    },
  ];
  switch (data.type) {
    case 1:
      //返回权限菜单
      menuList.forEach((item,index)=>{
        if(item.type!=1){
          menuList.splice(index,1);
        }
        if(item.children&&item.children.length>0){
          item.children.forEach((itemMenu,index)=>{
            if(itemMenu.type!=1){
              item.children.splice(index,1);
            }
          })
        }
      })
      response.data = menuList;
      return response;
      break;
    case 2:
      //返回权限菜单
      menuList.forEach((item,index)=>{
        if(item.type!=2){
          menuList.splice(index,1);
        }
        if(item.children&&item.children.length>0){
          item.children.forEach((itemMenu,index)=>{
            if(itemMenu.type!=2){
              item.children.splice(index,1);
            }
          })
        }
      })
      response.data = menuList;
      return response;
      break;
    case 3:
      //返回权限菜单
      menuList.forEach((item,index)=>{
        if(item.type!=3){
          menuList.splice(index,1);
        }
        if(item.children&&item.children.length>0){
          item.children.forEach((itemMenu,index)=>{
            if(itemMenu.type!=3){
              item.children.splice(index,1);
            }
          })
        }
      })
      response.data = menuList;
      return response;
    default:
      response.data = menuList;
      return response;
  }

}
Mock.mock('/api/user/userLogin', 'post', userLogin);
Mock.mock('/api/user/getMenu', 'post', menuList);
